# Obtain command line arguments from .sh file

args=(commandArgs(TRUE))

# Evaluate the arguments for use in this script

for(i in 1:length(args)) 
	
	{
		
	eval(parse(text=args[[i]]))
 
	}
	

### Create directories

in.dir = '/home1/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/microCLIMweeklyaggregate/'
setwd(in.dir)
out.dir = '/home1/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/microCLIMweeklystep2/'

# Load libraries

library('SDMTools')

### List all yearly summary files

maxfiles = list.files(in.dir,recursive=T,full.names=T,pattern='meanmax')
minfiles = list.files(in.dir,recursive=T,full.names=T,pattern='meanmin')

### Create blank objects to bind data to

maxmonthsummary1 = NULL
minmonthsummary1 = NULL
maxmonthsummary2 = NULL
minmonthsummary2 = NULL
maxmonthsummary3 = NULL
minmonthsummary3 = NULL
maxmonthsummary4 = NULL
minmonthsummary4 = NULL


for (i in c(1:30)) # This loop will extract each individual month (1 column) from the yearly summary, and bind them into a separate dataframe

	{
	
	for (skip in c(100000,200000,300000,400000))
	
		{
	
		max.data = read.csv(maxfiles[i], header=F, skip=1, nrows=skip)
		
		if(skip==100000)
		
		{
		
		maxmonthsummary1 = cbind(maxmonthsummary1,max.data[,wk])
		
		}
		
		if(skip==200000)
		
		{
		
		maxmonthsummary2 = cbind(maxmonthsummary2,max.data[,wk])
		
		}
		
		if(skip==300000)
		
		{
		
		maxmonthsummary3 = cbind(maxmonthsummary3,max.data[,wk])
		
		}
		
		if(skip==400000)
		
		{
		
		maxmonthsummary4 = cbind(maxmonthsummary4,max.data[,wk])
		
		}
	
	if(i==30) 
	
	{
	
	write.csv(maxmonthsummary1,file=paste(out.dir,'max_summary_week_',wk,'_part',1,'.csv',sep=''),row.names=F)
	write.csv(maxmonthsummary2,file=paste(out.dir,'max_summary_week_',wk,'_part',2,'.csv',sep=''),row.names=F)
	write.csv(maxmonthsummary3,file=paste(out.dir,'max_summary_week_',wk,'_part',3,'.csv',sep=''),row.names=F)
	write.csv(maxmonthsummary4,file=paste(out.dir,'max_summary_week_',wk,'_part',4,'.csv',sep=''),row.names=F)
	
	rm(max.data))
	
	rm(list=(ls()[grep('maxmonthsummary',ls())]))
	
	
	}
	
	cat('\n',i,'\n')
			
	}	
	

for (i in c(1:30)) # This loop will extract each individual month (1 column) from the yearly summary, and bind them into a separate dataframe

	{
	
	for (skip in c(100000,200000,300000,400000))
	
		{
	
		min.data = read.csv(minfiles[i], header=F, skip=1, nrows=skip)
		
		if(skip==100000)
		
		{
		
		minmonthsummary1 = cbind(minmonthsummary1,min.data[,wk])
		
		}
		
		if(skip==200000)
		
		{
		
		minmonthsummary2 = cbind(minmonthsummary2,min.data[,wk])
		
		}
		
		if(skip==300000)
		
		{
		
		minmonthsummary3 = cbind(minmonthsummary3,min.data[,wk])
		
		}
		
		if(skip==400000)
		
		{
		
		minmonthsummary4 = cbind(minmonthsummary4,min.data[,wk])
		
		}
	
	if(i==30) 
	
	{
	
	write.csv(minmonthsummary1,file=paste(out.dir,'min_summary_week_',wk,'_part',1,'.csv',sep=''),row.names=F)
	write.csv(minmonthsummary2,file=paste(out.dir,'min_summary_week_',wk,'_part',2,'.csv',sep=''),row.names=F)
	write.csv(minmonthsummary3,file=paste(out.dir,'min_summary_week_',wk,'_part',3,'.csv',sep=''),row.names=F)
	write.csv(minmonthsummary4,file=paste(out.dir,'min_summary_week_',wk,'_part',4,'.csv',sep=''),row.names=F)
	
	rm(min.data))
	
	rm(list=(ls()[grep('minmonthsummary',ls())]))
	
	}
	
	cat('\n',i,'\n')
			
	}	
	